<template>
  <el-dialog
    title="分配角色"
    :visible="isShow"
    width="700px"
    @close="cancelEvent"
  >
    <h1>{{ checkedValue }}</h1>
    <el-checkbox
      v-for="item in list"
      :key="item.id"
      v-model="checkedValue"
      :label="item.id"
    >{{ item.name }}</el-checkbox>

    <template #footer>
      <div style="text-align: center">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="cancelEvent">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysRole } from '@/api/setting'
import { sysUser } from '@/api/user'
import { sysUserAssignRoles } from '@/api/employees'
export default {
  model: {
    prop: 'isShow'
  },
  props: {
    isShow: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      list: [],
      checkedValue: [],
      id: ''
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysRole({
        page: 1,
        pagesize: 9999
      })
      this.list = res.data.rows
      console.log('角色列表', res)
    },
    cancelEvent() {
      this.$emit('input', false)
      // 清空已选择
      this.checkedValue = []
    },
    async getUserRole(id) {
      this.id = id
      const res = await sysUser(id)
      // 回显角色
      this.checkedValue = res.data.roleIds
      console.log('getUserRole', res)
    },
    // 点击确定
    async submit() {
      await sysUserAssignRoles({
        roleIds: this.checkedValue,
        id: this.id
      })
      this.$message.success('分配角色成功')
      this.cancelEvent()
    }
  }
}
</script>
<style></style>
